package 哈夫曼编码;

import java.util.PriorityQueue;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        PriorityQueue<Long> p = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            long x = in.nextLong();
            p.offer(x);
        }
        long res = 0;
        while (p.size() > 1) {
            long t1 = p.poll();
            long t2 = p.poll();
            res += t1 + t2;
            p.offer(t1 + t2);
        }
        System.out.println(res);
    }
}
